<?php

require FCPATH . 'application/controllers/backend/backend.php';

class Member extends Backend {

    public function __construct() {
        parent::__construct();

        $this->load->model('member_model', 'model');
    }
    
    public function index() {
        $this->load_javascripts('backend_member');

        $this->load_stylesheets(array(
            'global',
            'backend_form'
        ));

        $this->rend('member_index_view');
    }

    public function find() {
        $id = intval($this->uri->segment(4));

        $data = $this->model->find($id);

        if ($data) {
            $this->json_back('XT001', '', $data);
        } else {
            $this->json_back('XT002', '数据库错误');
        }
    }

    public function records() {
        $page = intval($this->input->get('page'));
        $page_size = intval($this->input->get('pageSize'));
        $page = $page ? $page : 1;
        $page_size = $page_size ? $page_size : 20;

        $offset = ($page - 1) * $page_size;

        $this->json_back('XT001', '', array(
            'total' => $this->model->total(),
            'records' => $this->model->records($page_size, $offset)
        ));
    }

    public function insert() {
        $data = array(
            'label' => trim($this->input->post('label')),
            'label_en' => trim($this->input->post('label_en')),
            'link' => trim($this->input->post('link'))
        );

        if (empty($data['label'])) {
            $this->json_back('XT002', '标签不能为空');
        }

        if (empty($data['label_en'])) {
            $this->json_back('XT002', '英文标签不能为空');
        }
        
        if (empty($data['link'])) {
            $this->json_back('XT002', '链接不能为空');
        }

        $conditions = array(
            array('label', $data['label'])
        );

        if ($this->model->total($conditions) > 0) {
            $this->json_back('XT002', '已经存在，请更换一个');
        }

        if ($this->model->insert($data)) {
            $this->json_back('XT001');
        } else {
            $this->json_back('XT002', 'database error.');
        }
    }

    public function update() {
        $id = intval($this->input->post('id'));

        $data = array(
            'label' => trim($this->input->post('label')),
            'label_en' => trim($this->input->post('label_en')),
            'link' => trim($this->input->post('link'))
        );

        if (empty($data['label'])) {
            $this->json_back('XT002', '值不能为空');
        }

        if (empty($data['label_en'])) {
            $this->json_back('XT002', '英文值不能为空');
        }
		
		if (empty($data['link'])) {
            $this->json_back('XT002', '链接不能为空');
        }

        if (!($label = $this->model->find($id))) {
            $this->json_back('XT002', '已被删除');
        }

        $conditions = array(
            array('label', $data['label']),
            array('id <>', $id)
        );

        if ($this->model->total($conditions) > 0) {
            $this->json_back('XT002', '已经存在，请更换一个');
        }

        if ($this->model->update($id, $data)) {
            $this->json_back('XT001');
        } else {
            $this->json_back('XT002', 'database error.');
        }
    }
    
    public function delete() {
        $id = intval($this->uri->segment(4));

        if (!$this->model->find($id)) {
            $this->json_back('XT002', '记录不存在，已被删除');
        }

        if ($this->model->delete($id)) {
            $this->json_back('XT001');
        } else {
            $this->json_back('XT002', 'database error.');
        }
    }

}